package com.hsd.znsh.converter;

import com.hsd.znsh.entity.User;
import com.hsd.znsh.enums.UserRoleEnum;
import com.hsd.znsh.pojo.output.UserInfoVO;
import com.hsd.znsh.pojo.output.UserVO;
import com.hsd.znsh.repository.UserRepository;
import com.hsd.znsh.utils.BeanUtil;
import com.hsd.znsh.utils.EnumUtil;

import java.util.ArrayList;
import java.util.List;

public class UserConverter {

    public static UserVO convertToUserVO(User user, UserRepository userRepository){
        UserVO userVO=new UserVO();
        BeanUtil.copyProperties(user,userVO);
        if(user.getCreateUserId()!=null){
            userVO.setCreateUsername(userRepository.findOne(user.getCreateUserId()).getUsername());
        }
        userVO.setUserType(EnumUtil.getMessageByCode(user.getUserType(), UserRoleEnum.class));
        return userVO;
    }

    public static UserInfoVO convertToUserInfoVO(User user, UserRepository userRepository){
        UserInfoVO userInfoVO=new UserInfoVO();
        BeanUtil.copyProperties(user,userInfoVO);
        if(user.getCreateUserId()!=null){
            userInfoVO.setCreateUsername(userRepository.findOne(user.getCreateUserId()).getUsername());
        }
        return userInfoVO;
    }

    public static List<UserVO> convertToUserVOList(List<User> users,UserRepository userRepository){
        List<UserVO> list=new ArrayList<>();
        for(User user:users){
            list.add(convertToUserVO(user,userRepository));
        }
        return list;
    }


}
